package eva.savant.business.ventas.view

import eva.savant.businessobject.mt.Material
import eva.savant.businessobject.vt.PedidoDetalle
import eva.savant.businessobject.vt.Venta
import eva.savant.businessobject.vt.Cliente
import eva.savant.businessobject.vt.VentaDetalle

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.faces.convert.Converter
import org.primefaces.model.chart.AxisType
import org.primefaces.model.chart.ChartSeries
import org.primefaces.model.chart.CategoryAxis
import org.primefaces.model.chart.LineChartModel

@SessionScoped
@ManagedBean(name = 'IIVentasClienteChart')
public class IIVentasClienteChart extends Bean {

  Date dateIni, dateEnd; def pedido= false

  def material, XMaterialCod= '', cantidad= 0, model

  @PostConstruct
  public void cleanEntity() {

    model=  null

    pedido= false

    cantidad= null

    XMaterialCod= ''

    material= new Material()


    lsEntity = null

    entity = new Venta()

    dateIni= dateEnd= new Date()
  }

  public List findLsEntity() { model= null

    dateIni= toStartDay(dateIni); dateEnd= toMidnight(dateEnd); form= true

    if(entity.cliente?.id == null || material?.id == null) return null

    def x= pedido? 'pedido': 'venta', clase= pedido? PedidoDetalle : VentaDetalle


    def d= get(clase,"sum(cantidad), sum(importe), sum(descuento), sum(total): ${x}.operacionId > 0 and ${x}.cliente.id=? and material.id=? and ${x}.fecha between ? and ?", entity.cliente.id, material.id, dateIni, dateEnd)

    cantidad= d[0]; entity.importe= d[1]; entity.descuento= d[2]; entity.total= d[3]

    lsEntity= find(clase,"${x}.fecha, sum(cantidad), sum(importe), sum(descuento), sum(total): " +

                         "${x}.operacionId > 0 and ${x}.cliente.id=? and material.id=? and ${x}.fecha between ? and ? group by ${x}.fecha order by ${x}.fecha", entity.cliente.id, material.id, dateIni, dateEnd)

    buildChart(); return lsEntity
  }

  public buildChart() {

    model = new LineChartModel()

    model.setLegendPosition('n')

    model.setShowPointLabels(true)

    model.getAxes().put(AxisType.X, new CategoryAxis('Fecha'))

    model.getAxis(AxisType.Y).setLabel('Cantidad')

    model.getAxis(AxisType.X).setTickAngle(90)


    def c= new ChartSeries()

    c.setLabel material.nombre + ''

    for(x in lsEntity) c.set x[0].format('dd.M'), x[1]

    model.addSeries c
  }

  public List complete(String value) { return completeLike(Cliente.class,'persona.nombre',value) }

  public Converter getConverter() { return converterLike(Cliente.class) }

  def XMaterialFind() {

    material= get(Material.class,'rapido=?',toInteger(XMaterialCod))

    if(material == null) material= get(Material.class, XMaterialCod)

    if(material == null) material= get(Material.class,'barras=?', XMaterialCod)

    if(material != null) XMaterialCod= material.codigo
  }

  def XMaterialSelect() { XMaterialCod= material.codigo }

  def getXMaterialConverter() { return converterLike(Material.class) }

  def XMaterialComplete(String value) { return completeLike(Material.class,'nombre',value) }

}
